home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
common
/
actions.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
9KB
|
245 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from __future__ import with_statement
__author__ = 'dotSyntax'
import util
import util.data_importer as importer
from util import baseclasses, do, curly, import_function, funcinfo
from functools import partial
from types import FunctionType as function
from collections import defaultdict
from util.observe import Observable, ObservableMeta
import logging
log = logging.getLogger('actions')
import wx
import sys
import syck
from wx import BitmapFromImage
_actions_cached = None
_actions_imported = False
def _actions():
global _actions_imported
if _actions_imported:
return _actions_cached
skin = skin
import gui
try:
actions = importer.yaml_import('actions', loadpath = [
skin.resourcedir()])
except ImportError:
actions = { }
finally:
_actions_imported = True
add_actions(actions)
return _actions_cached
def add_actions(actionsdict):
global _actions_cached
the_actions = _actions()
if the_actions is None:
the_actions = { }
the_actions = util.merge.merge(the_actions, actionsdict)
_actions_cached = the_actions
def forclass(cls, search_bases = True):
if not isinstance(cls, type) and hasattr(cls, '__class__'):
cls = cls.__class__
if search_bases:
classes = [
cls] + baseclasses(cls)
classes.reverse()
else:
classes = [
cls]
return util.flatten((lambda .0: for c in .0:
_actions().get(c.__name__, []))(classes))
class Action(Observable):
def __init__(self, name, callable):
Observable.__init__(self)
util.autoassign(self, locals())
def __call__(self, *args, **kws):
return self.callable(*args, **kws)
def __repr__(self):
return '<Action %s for %r>' % (self.name, self.callable)
def menu(parent, obj, menu = None, cls = None, search_bases = True, filter = (lambda func: True)):
skin = skin
import gui
UMenu = UMenu
import gui.uberwidgets.umenu
actions = None(forclass if cls is not None else obj, search_bases)
menu = None if menu is not None else UMenu(parent)
names = set()
for action in actions:
if isinstance(action, basestring) and action.startswith('--'):
menu.AddSep()
continue
elif 'method' in action:
import_function(action['method'])(menu, obj)
continue
name = action['call']
if name in names:
continue
else:
names.add(name)
func = getattr(obj, name, None)
if func is None:
continue
bitmap = None if 'bitmap' in action else None
gui_name = action['name']
try:
(precondition, needslist) = getattr(obj, '_actions')[name]
except KeyError:
e = None
precondition = lambda v: True
needslist = []
if needslist:
import gui.userform as userform
callback = partial(userform.getinput, obj, parent, needslist, func, title = gui_name.replace('&', ''))
elif 'gui' in action:
gui = import_function(action['gui'])
if not hasattr(gui, 'Prompt'):
callback = lambda gui = gui, func = (func,): gui(obj, func)
else:
def callback(gui = gui, func = (func,)):
gui(None, obj).Prompt(func)
else:
callback = func
result = None if precondition is not None else True
if filter(func):
if precondition is None or result is not None:
name = action_name(obj, gui_name)
menu.AddItem(name, bitmap = bitmap, callback = callback).Enable(bool(result))
continue
if menu.GetMenuItemCount() == 0:
menu.AddItem(_('No actions')).Enable(False)
while menu[-1].IsSeparator():
menu.RemoveItem(menu[len(menu) - 1])
return menu
def action_name(obj, name):
if name.find('$') != -1:
name = curly(name, source = {
'obj': obj })
return name
_actioncalls = dict()
class ActionMeta(type):
action_prefix = '__ACTION__'
class_action_attr = '_actions'
def __init__(cls, name, bases, dict):
super(ActionMeta, cls).__init__(name, bases, dict)
superactions = util.odict()
if 'inherited_actions' in dict:
bases = bases + tuple(dict['inherited_actions'])
(do,)((lambda .0: for c in .0:
if hasattr(c, '_actions'):
superactions.update(c._actions)continue)(bases))
setattr(cls, ActionMeta.class_action_attr, superactions)
actions = getattr(cls, ActionMeta.class_action_attr)
prefix = ActionMeta.action_prefix
for v in dict.itervalues():
if isinstance(v, function) and v.__name__.startswith(prefix):
v.__name__ = v.__name__[len(prefix):]
val = _actioncalls.pop(v)
actions[v.__name__] = val
continue
class ActionType(object):
__metaclass__ = ActionMeta
class ObservableActionMeta(ObservableMeta, ActionMeta):
pass
class ActionError(Exception):
pass
def _action_err_info(callable_predicate):
code = callable_predicate.func_code
fn = code.co_filename
ln = code.co_firstlineno
getline = getline
import linecache
if not getline(fn, ln):
pass
line = '<UNKNOWN>'
return (fn, ln, line)
def action(callable_predicate = None, needs = None):
if callable_predicate is not None and not callable(callable_predicate):
raise TypeError("action decorator needs a callable or None as it's only argument (you gave a %s)" % type(callable_predicate))
def action_dec(meth):
def func(*args, **kws):
if callable_predicate:
(filename, line, line_text) = _action_err_info(callable_predicate)
err_msg = '%s is not allowed, %%s\nFile "%s", line %d, in %s\n%s' % (meth.__name__, filename, line, callable_predicate.__name__, line_text)
try:
allow = callable_predicate(*args, **kws)
except TypeError:
e = None
raise ActionError, err_msg % 'error in precondition function: ' + str(e)
if not allow:
log.warning(str(ActionError(err_msg % 'precondition not met for')))
return meth(*args, **kws)
func.__name__ = ActionMeta.action_prefix + meth.__name__
func.__doc__ = meth.__doc__
if callable_predicate:
func.action_allowed = callable_predicate
else:
func.action_allowed = lambda *a, **k: True
tneeds = needs
_actioncalls[func] = (callable_predicate, tneeds)
return func
return action_dec